#include "camera.h"

#include <iostream>

#define INCREMENT 5

camera::camera()
{

  x = -10.0f;
  y = -40;
  z = -150;
  roll = 0.0;
  pitch = -20.0;
  yaw = 0.0f;

  rotating = false;

}

void camera::setUp(){
    moveBack();
    moveBack();
    moveBack();
    moveUp();
    moveRight();
}

void camera::moveUp(){

  if (!rotating) {
    y -= INCREMENT;
  } else {
    roll += 10.0;
  }

}

void camera::moveDown(){

  if (!rotating) {
    y += INCREMENT;
  } else {
    roll -= 10.0;
  }

}

void camera::moveLeft(){

  if (!rotating) {
    x += INCREMENT;
  } else {
    yaw += 10.0;
  }

}

void camera::moveRight(){

  if (!rotating) {
    x -= INCREMENT;
  } else {
    yaw -= 10.0;
  }

}

void camera::moveForward(){

  if (!rotating) {
    z += cos(yaw * (3.14159/180.0)) * INCREMENT;
    x += sin(yaw * (3.14159/180.0)) * INCREMENT;
    y -= tan(pitch * (3.14159/180.0)) * INCREMENT;
  } else {
    pitch += 10.0;
  }

}

void camera::moveBack(){
  if (!rotating) {
    z -= cos(yaw * (3.14159/180.0)) * INCREMENT;
    x -= sin(yaw * (3.14159/180.0)) * INCREMENT;
    y += tan(pitch * (3.14159/180.0)) * INCREMENT;
  } else {
    pitch -= 10.0;
  }

}

void camera::applyTransform() {

  // apply pitch
  glRotatef(pitch, x, 0.0, 0.0);

  // apply roll
  glRotatef(roll, 0.0, 0.0, z);
  
  // apply yaw
  glRotatef(yaw, 0.0, y, 0.0);

  std::cout << "Translating to: " << x << " " << y << " " << z << std::endl;
  glTranslatef(x, y, z);

}

void camera::toggleRotating() {
  rotating = !rotating;
}
